package 分类.链表;

public class 环形链表II {
    public ListNode detectCycle(ListNode head) {
        ListNode slow,fast;
        slow=fast=head;
        while (fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow) break;
        }
        if(fast==null||fast.next==null) return null;

        slow=head;
        while (fast!=slow){
            slow=slow.next;
            fast=fast.next;
        }
        return slow;
    }
}
